;
(function() {
	var skill = {
		/*
		 * 推荐返利
		 */
		state_weishenqingduihuan_to_shenqingduihuanzhong: function(bus_id, apply_acc) {
			var bus_user_bind = BusUserBindCon.get(BusUserBindCon.build({
				bus_id: bus_id,
				user_id: this._id
			}));
			console.log("bus_user_bind.state", bus_user_bind.state);
			if (!bus_user_bind.state) {
				bus_user_bind.state = BusUserBindCon.STATE.未申请兑换;
			}
			if (bus_user_bind.state == BusUserBindCon.STATE.未申请兑换) {
				bus_user_bind._nextFlowStatus();
				//目前默认全部兑换
				bus_user_bind.apply_acc = apply_acc || bus_user_bind.acc;
				bus_user_bind.save();
			} else {
				throwCusError("兑现所在状态无法进行相应转换");
			}
			bus_user_bind.bus = BusCon.get(bus_user_bind.bus_id);
			if (!bus_user_bind.acc) {
				throwCusError("无可兑换的红利")
			}
			bus_user_bind._money = bus_user_bind.bus.recommendAccToMoney(bus_user_bind.acc);
			return bus_user_bind;
		},
		state_shangjiayijingfukuan_to_yonghuquerenfukuan: function(bus_id) {
			var bus_user_bind = BusUserBindCon.get(BusUserBindCon.build({
				bus_id: bus_id,
				user_id: this._id
			}));
			if (!bus_user_bind.state) {
				bus_user_bind.state = BusUserBindCon.STATE.未申请兑换;
			}
			if (bus_user_bind.state == BusUserBindCon.STATE.商家已经付款) {
				bus_user_bind._nextFlowStatus();
				bus_user_bind.total_acc = bus_user_bind.total_acc || 0;
				bus_user_bind.total_acc += bus_user_bind.apply_acc;
				bus_user_bind.acc -= bus_user_bind.apply_acc;
				bus_user_bind.state = BusUserBindCon.STATE.未申请兑换;
				bus_user_bind.save();
			} else {
				throwCusError("兑现所在状态无法进行相应转换");
			}
			bus_user_bind.bus = BusCon.get(bus_user_bind.bus_id);
			bus_user_bind._money = bus_user_bind.bus.recommendAccToMoney(bus_user_bind.acc);
			return bus_user_bind;
		},
		getAccBus: function(num, page) {
			var acc_infos = BusUserBindCon.findByUserId(this._id);
			acc_infos = data_cut.page(acc_infos, num, page);
			acc_infos = acc_infos.map(function(bus_user_bind) {
				bus_user_bind = $Object.clone(bus_user_bind);
				// bus_user_bind.user = UserCon.get(bus_user_bind.user_id);
				bus_user_bind.bus = BusCon.get(bus_user_bind.bus_id);
				//计算可兑换的金额
				bus_user_bind._money = bus_user_bind.bus.recommendAccToMoney(bus_user_bind.acc);
				console.log(bus_user_bind.state);
				return bus_user_bind;
			});
			return acc_infos;
		},
		getAccInstanceByBudId: function(bus_id) {
			var result;
			var acc_infos = BusUserBindCon.findByUserId(this._id);
			acc_infos.every(function(bus_user_bind) {
				if (bus_user_bind.bus_id == bus_id) {
					result = BusUserBindCon.get(bus_user_bind._id);
					return false;
				} else {
					return true;
				}
			});
			return result;
		},
		getAccBusById: function(bus_id) {
			var result = this.getAccInstanceByBudId(bus_id);
			if (result) {
				result = $Object.clone(result);
				result.bus = BusCon.get(result.bus_id);
				//计算可兑换的金额
				result._money = result.bus.recommendAccToMoney(result.acc);
			}
			return result;
		},
		//为订单使用推荐返利的抵扣金额
		bindAccToAch: function(bind_acc, ach_id) {
			var ach = this._getAch(ach_id);
			return ach._bindAcc(bind_acc);
		},
		acc_to_BusUser_money: function(bus_id, money) {
			console.log(bus_id, money)
			var bus_user_bind = BusUserBindCon.get(BusUserBindCon.build({
				bus_id: bus_id,
				user_id: this._id
			}));
			bus_user_bind._to_BusUser_money(money);
			return bus_user_bind
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var CouponCon = require("../coupon/_con");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusUserCon = require("../bus_user/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());